package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.pojo.dto.FormGetDTO;
import com.xbongbong.paas.pojo.dto.NeedApproveDTO;
import com.xbongbong.paas.pojo.dto.ProcessArchiveDTO;
import com.xbongbong.paas.pojo.dto.ProcessDeleteDTO;
import com.xbongbong.paas.pojo.dto.ProcessEnableDTO;
import com.xbongbong.paas.pojo.dto.ProcessGetDTO;
import com.xbongbong.paas.pojo.dto.ProcessOpenDTO;
import com.xbongbong.paas.pojo.dto.ProcessParamsDTO;
import com.xbongbong.paas.pojo.dto.ProcessPreviewDTO;
import com.xbongbong.paas.pojo.dto.ProcessUpdateDTO;
import com.xbongbong.paas.pojo.dto.ProcessVersionDTO;
import com.xbongbong.paas.pojo.vo.NeedApproveVO;
import com.xbongbong.paas.pojo.vo.ProcessArchiveVO;
import com.xbongbong.paas.pojo.vo.ProcessDeleteVO;
import com.xbongbong.paas.pojo.vo.ProcessEnableVO;
import com.xbongbong.paas.pojo.vo.ProcessFormFieldVO;
import com.xbongbong.paas.pojo.vo.ProcessGetVO;
import com.xbongbong.paas.pojo.vo.ProcessOpenVO;
import com.xbongbong.paas.pojo.vo.ProcessParamsVO;
import com.xbongbong.paas.pojo.vo.ProcessUpdateVO;
import com.xbongbong.paas.pojo.vo.ProcessVersionVO;
import com.xbongbong.paas.service.PaasProcessTemplateService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * Description: 流程设计操作
 *
 * @author 李杰
 * @version v1.0
 * @date 2019/1/15 14:34
 * @since v1.0
 */
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/process")
public class PaasProcessTemplateController {

    @Resource
    private PaasProcessTemplateService paasProcessTemplateService;

    /**
     * 流程版本
     * @param processVersionDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/processVersion", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String processVersion(@RequestBody @Valid ProcessVersionDTO processVersionDTO, BindingResult br) {
        XbbResponse<ProcessVersionVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ProcessVersionVO processVersionVO = paasProcessTemplateService.processVersion(processVersionDTO);
                response = new XbbResponse<>(processVersionVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 开启流程
     *
     * @param processOpenDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/open", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String open(@RequestBody @Valid ProcessOpenDTO processOpenDTO, BindingResult br) {
        XbbResponse<ProcessOpenVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ProcessOpenVO processOpenVO = paasProcessTemplateService.open(processOpenDTO);
                response = new XbbResponse<>(processOpenVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 新增流程
     *
     * @param processGetDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String add(@RequestBody @Valid ProcessGetDTO processGetDTO, BindingResult br) {
        XbbResponse<ProcessGetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ProcessGetVO processGetVO = paasProcessTemplateService.add(processGetDTO);
                response = new XbbResponse<>(processGetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 关闭流程
     *
     * @param processDeleteDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String delete(@RequestBody @Valid ProcessDeleteDTO processDeleteDTO, BindingResult br) {
        XbbResponse<ProcessDeleteVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ProcessDeleteVO processDeleteVO = paasProcessTemplateService.delete(processDeleteDTO);
                response = new XbbResponse<>(processDeleteVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
    /**
     * 流程归档状态修改
     * @param processArchiveDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/archive", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String archive(@RequestBody @Valid ProcessArchiveDTO processArchiveDTO, BindingResult br) {
        XbbResponse<ProcessArchiveVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ProcessArchiveVO processArchiveVO = paasProcessTemplateService.archive(processArchiveDTO);
                response = new XbbResponse<>(processArchiveVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
    /**
     * 流程归档状态修改
     * @param processArchiveDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/cancelArchive", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String cancelArchive(@RequestBody @Valid ProcessArchiveDTO processArchiveDTO, BindingResult br) {
        XbbResponse<ProcessArchiveVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ProcessArchiveVO processArchiveVO = paasProcessTemplateService.cancelArchive(processArchiveDTO);
                response = new XbbResponse<>(processArchiveVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 获取流程
     *
     * @param processGetDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/get", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String get(@RequestBody @Valid ProcessGetDTO processGetDTO, BindingResult br) {
        XbbResponse<ProcessGetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ProcessGetVO processGetVO = paasProcessTemplateService.get(processGetDTO);
                response = new XbbResponse<>(processGetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 获取预览流程
     *
     * @param processPreviewDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/processPreview", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String processPreview(@RequestBody @Valid ProcessPreviewDTO processPreviewDTO, BindingResult br) {
        XbbResponse<ProcessGetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ProcessGetVO processGetVO = paasProcessTemplateService.processPreview(processPreviewDTO);
                response = new XbbResponse<>(processGetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 保存流程相关信息
     *
     * @param processUpdateDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String save(@RequestBody @Valid ProcessUpdateDTO processUpdateDTO, BindingResult br) {
        XbbResponse<ProcessUpdateVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ProcessUpdateVO processUpdateVO = paasProcessTemplateService.save(processUpdateDTO);
                response = new XbbResponse<>(processUpdateVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 开启/关闭流程
     *
     * @param processEnableDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/enable", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String enable(@RequestBody @Valid ProcessEnableDTO processEnableDTO, BindingResult br) {
        XbbResponse<ProcessEnableVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ProcessEnableVO processEnableVO = paasProcessTemplateService.enable(processEnableDTO);
                response = new XbbResponse<>(processEnableVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 启用标记
     *
     * @param processEnableDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/enableFlag", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String enableFlag (@RequestBody @Valid ProcessEnableDTO processEnableDTO, BindingResult br) {
        XbbResponse<ProcessEnableVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ProcessEnableVO processEnableVO = paasProcessTemplateService.enable(processEnableDTO);
                response = new XbbResponse<>(processEnableVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 获取需审批字段
     *
     * @param needApproveDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/needApproveFields", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String needApproveFields(@RequestBody @Valid NeedApproveDTO needApproveDTO, BindingResult br) {
        XbbResponse<NeedApproveVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                NeedApproveVO needApproveVO = paasProcessTemplateService.needApproveFields(needApproveDTO);
                response = new XbbResponse<>(needApproveVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 获取节点中
     *
     * @param formGetDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/formFields", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String formFields(@RequestBody @Valid FormGetDTO formGetDTO, BindingResult br) {
        XbbResponse<ProcessFormFieldVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ProcessFormFieldVO processFormFieldVO = paasProcessTemplateService.formFields(formGetDTO);
                response = new XbbResponse<>(processFormFieldVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 切换到流程设定tab时，先请求此接口，再用返回数据请求原来的接口
     * 主要是因为付款管理（回款管理）切换是付款单（回款单）的审批，需要对参数进行处理
     *
     * @param processParamsDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/processParams", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String get(@RequestBody @Valid ProcessParamsDTO processParamsDTO, BindingResult br) {
        XbbResponse<ProcessParamsVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ProcessParamsVO processParams = paasProcessTemplateService.processParams(processParamsDTO);
                response = new XbbResponse<>(processParams);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

}
